package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.MedicalRecordBo;
import com.hospital.workstation.domain.vo.MedicalRecordVo;
import com.hospital.workstation.domain.MedicalRecord;
import com.hospital.workstation.mapper.MedicalRecordMapper;
import com.hospital.workstation.service.IMedicalRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 药历用药汇总Service业务层处理
 *
 * @author whb
 * @date 2023-02-13
 */
@RequiredArgsConstructor
@Service
public class MedicalRecordServiceImpl implements IMedicalRecordService {

    private final MedicalRecordMapper baseMapper;

    /**
     * 查询药历用药汇总
     */
    @Override
    public MedicalRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药历用药汇总列表
     */
    @Override
    public TableDataInfo<MedicalRecordVo> queryPageList(MedicalRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MedicalRecord> lqw = buildQueryWrapper(bo);
        Page<MedicalRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药历用药汇总列表
     */
    @Override
    public List<MedicalRecordVo> queryList(MedicalRecordBo bo) {
        LambdaQueryWrapper<MedicalRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MedicalRecord> buildQueryWrapper(MedicalRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MedicalRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, MedicalRecord::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPurpose()), MedicalRecord::getPurpose, bo.getPurpose());
        lqw.like(StringUtils.isNotBlank(bo.getPillsName()), MedicalRecord::getPillsName, bo.getPillsName());
        lqw.eq(StringUtils.isNotBlank(bo.getAmount()), MedicalRecord::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getIsContinue()), MedicalRecord::getIsContinue, bo.getIsContinue());
        lqw.eq(bo.getBeginDate() != null, MedicalRecord::getBeginDate, bo.getBeginDate());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicalType()), MedicalRecord::getMedicalType, bo.getMedicalType());
        return lqw;
    }

    /**
     * 新增药历用药汇总
     */
    @Override
    public Boolean insertByBo(MedicalRecordBo bo) {
        MedicalRecord add = BeanUtil.toBean(bo, MedicalRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改药历用药汇总
     */
    @Override
    public Boolean updateByBo(MedicalRecordBo bo) {
        MedicalRecord update = BeanUtil.toBean(bo, MedicalRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MedicalRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药历用药汇总
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
